import r from '../helper/config.js'
import {loginApi} from '../api/apis.js'

export function login(data={}){
	return new Promise((resolve,reject)=>{
		uni.login({
			success:async res=> {
				const params = {
					code:res.code,
					pid:r.pid,
					pageParamStorage:JSON.stringify(data),
					...data
				}
				const result = await loginApi(params)
				uni.setStorageSync('token', result.data.token)
				uni.setStorageSync('key', result.data.key)
				uni.setStorageSync('cid', result.data.cid)
				uni.setStorageSync('sid', result.data.sid)
				resolve(result.data.token)
			},
			fail:err=>{
				uni.showToast({
					title: '未知错误',
					icon: 'none'
				})
				reject(err)
			}
		})
	})
}